<template>
  <el-dialog v-if="load" title="调整采购" v-model="visible" append-to-body :close-on-click-modal="false" width="500px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <el-form-item label="采购人员" prop="purchase_user_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <VCascader v-model="form.purchase_user_id" placeholder="请选择" class="w-full" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (p_ids) => {
  if (p_ids && !p_ids.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  form.value = { p_ids, purchase_user_id: '' }
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async() => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api.updatePurchase(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>